//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    expressNu: null,
    expressInfo: null,
    index:0,
    sourceList: [
      { "name": "圆通", "val": "getYtInfo" },
      { "name": "韵达", "val": "getYdInfo" }
      // { "name": "申通", "val": "getShtInfo" },
      // { "name": "顺丰", "val": "getShfInfo" },
      // { "name": "百世快递", "val": "getBsInfo" },
      // { "name": "邮政EMS", "val": "getEMSInfo" }
      ],
    currentMethod:'getYtInfo'
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  btnClick: function () {
    var thispage = this;
    thispage.setData({ expressInfo: '' });
    if (null == this.data.expressNu){
      wx.showToast({
        title: '请输入运单号！',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    console.log(this.data.currentMethod);
    var med = this.data.currentMethod;
    switch (med){
      case 'getYtInfo':
        app.getYtInfo(this.data.expressNu, function (data) {
          thispage.setData({ expressInfo: data });
        });
        break;
      case 'getYdInfo':
        console.log('韵达');
        wx.showToast({
          title: '暂时只支持圆通的查询哦！',
          icon: 'none',
          duration: 2000
        });
        break;
      case 'getShtInfo':
        app.getYdInfo(this.data.expressNu, function (data) {
          thispage.setData({ expressInfo: data });
        });
        break;
      case 'getShfInfo':
        app.getYdInfo(this.data.expressNu, function (data) {
          thispage.setData({ expressInfo: data });
        });
        break;
      case 'getBsInfo':
        app.getYdInfo(this.data.expressNu, function (data) {
          thispage.setData({ expressInfo: data });
        });
        break;
      case 'getEMSInfo':
        app.getYdInfo(this.data.expressNu, function (data) {
          thispage.setData({ expressInfo: data });
        });
        break;
      default:
        console.log('未知快递！');
        app.alertModel('未知快递！','未支持该快递的查询，\r\n请留言反馈哦~o~');
        break;
    }
  },
  input:function (e) {
    this.setData({expressNu:e.detail.value})
  },
  bindPickerChange:function(e){
    var v = this.data.sourceList[e.detail.value].val;
    this.setData({ currentMethod:v});
    this.setData({ index: e.detail.value });
  },




  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
